/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
    let stack = []  //初始化栈
    const map = {
        ')': '(',
        ']': '[',
        '}': '{'
    }
    for (let char of s) {
        if (char === '(' || char === '[' || char === '{') {
            stack.push(char);  //遇到左括号就入栈
        } else {
            //遇到右括号就判断栈顶元素是否与当前右括号匹配
            if (stack.length === 0 || stack.pop() !== map[char]) {
                return false;  //栈为空或者栈顶元素与当前右括号不匹配，返回false
            }
        }

    }
    return stack.length === 0;  //最后判断栈是否为空，为空则返回true，否则返回false
};